package edu.uclmgmu.bacteriosec.aspects.com.mysql.jdbc.PreparedStatement.setString;

import com.mysql.jdbc.*;
import java.sql.*;
import edu.uclmgmu.bacteriosec.aspects.common.Store;

privileged aspect Connection_Booleans {
	
	/** boolean */
	boolean around(com.mysql.jdbc.PreparedStatement caller) : target(caller) && 
	call(boolean com.mysql.jdbc.PreparedStatement.createStreamingResultSet()) {
		String currentJoinPoint = Store.getCurrentjoinPoint();
		String signature=thisJoinPointStaticPart.getSignature().toString();
		if (currentJoinPoint.equals(signature)) {
			int numberOfCallInTurn = Store.getNumberOfCallInTurn(signature);
			int numberOfCurrentCall = Store.getNumberOfCurrentCall(signature);
			if (numberOfCallInTurn == numberOfCurrentCall) {
				Store.updateNumberOfCurrentCall(signature);
				return !proceed(caller);
			} else {
				Store.updateNumberOfCurrentCall(signature);
				return proceed(caller);
			}
		} else {
			return proceed(caller);
		}
	}	


	boolean around(com.mysql.jdbc.RowData caller) : target(caller) && 
	call(boolean com.mysql.jdbc.RowData.hasNext()) {
		String currentJoinPoint = Store.getCurrentjoinPoint();
		String signature=thisJoinPointStaticPart.getSignature().toString();
		if (currentJoinPoint.equals(signature)) {
			int numberOfCallInTurn = Store.getNumberOfCallInTurn(signature);
			int numberOfCurrentCall = Store.getNumberOfCurrentCall(signature);
			if (numberOfCallInTurn == numberOfCurrentCall) {
				Store.updateNumberOfCurrentCall(signature);
				return !proceed(caller);
			} else {
				Store.updateNumberOfCurrentCall(signature);
				return proceed(caller);
			}
		} else {
			return proceed(caller);
		}
	}	
	
	boolean around(com.mysql.jdbc.StringUtils caller, String a, String b) : target(caller) &&
	args(a, b) && 
	call(boolean com.mysql.jdbc.StringUtils.startsWithIgnoreCase(String, String)) {
		String currentJoinPoint = Store.getCurrentjoinPoint();
		String signature=thisJoinPointStaticPart.getSignature().toString();
		if (currentJoinPoint.equals(signature)) {
			int numberOfCallInTurn = Store.getNumberOfCallInTurn(signature);
			int numberOfCurrentCall = Store.getNumberOfCurrentCall(signature);
			if (numberOfCallInTurn == numberOfCurrentCall) {
				Store.updateNumberOfCurrentCall(signature);
				return !proceed(caller, a, b);
			} else {
				Store.updateNumberOfCurrentCall(signature);
				return proceed(caller, a, b);
			}
		} else {
			return proceed(caller, a, b);
		}
	}	
	
	/** int **/	
	int around(com.mysql.jdbc.CharsetMapping caller) : target(caller) && 
	call(int com.mysql.jdbc.CharsetMapping.getNumberOfCharsetsConfigured()) {
		String currentJoinPoint = Store.getCurrentjoinPoint();
		String signature=thisJoinPointStaticPart.getSignature().toString();
		if (currentJoinPoint.equals(signature)) {
			int numberOfCallInTurn = Store.getNumberOfCallInTurn(signature);
			int numberOfCurrentCall = Store.getNumberOfCurrentCall(signature);
			if (numberOfCallInTurn == numberOfCurrentCall) {
				Store.updateNumberOfCurrentCall(signature);
				return 0;
			} else {
				Store.updateNumberOfCurrentCall(signature);
				return proceed(caller);
			}
		} else {
			return proceed(caller);
		}
	}
	
	int around(com.mysql.jdbc.PreparedStatement caller, boolean a, boolean b) : target(caller) &&
	args(a, b) && 
	call(int com.mysql.jdbc.PreparedStatement.executeUpdate(boolean, boolean)) {
		String currentJoinPoint = Store.getCurrentjoinPoint();
		String signature=thisJoinPointStaticPart.getSignature().toString();
		if (currentJoinPoint.equals(signature)) {
			int numberOfCallInTurn = Store.getNumberOfCallInTurn(signature);
			int numberOfCurrentCall = Store.getNumberOfCurrentCall(signature);
			if (numberOfCallInTurn == numberOfCurrentCall) {
				Store.updateNumberOfCurrentCall(signature);
				return proceed(caller, !a, !b);
			} else {
				Store.updateNumberOfCurrentCall(signature);
				return proceed(caller, a, b);
			}
		} else {
			return proceed(caller, a, b);
		}
	}

	int around(com.mysql.jdbc.RowData caller) : target(caller) && 
			call(* com.mysql.jdbc.RowData.size()) {
		String currentJoinPoint = Store.getCurrentjoinPoint();
		String signature=thisJoinPointStaticPart.getSignature().toString();
		if (currentJoinPoint.equals(signature)) {
			int numberOfCallInTurn = Store.getNumberOfCallInTurn(signature);
			int numberOfCurrentCall = Store.getNumberOfCurrentCall(signature);
			if (numberOfCallInTurn == numberOfCurrentCall) {
				Store.updateNumberOfCurrentCall(signature);
				return 0;
			} else {
				Store.updateNumberOfCurrentCall(signature);
				return proceed(caller);
			}
		} else {
			return proceed(caller);
		}
	}
	

//
//	
//	Object around() : call(* com.mysql.jdbc.*.*(..))&& 
//			!cflow(within(PreparedStatement_CountCalls)) && !within(edu.uclmgmu.bacteriosec.aspects.common.Store) 
//			&& !within(junit.*) {
//		Store.updateNumberOfCalls(thisJoinPointStaticPart.getSignature().toString());
//		System.out.println("mysql\t" + thisJoinPointStaticPart.getSignature().toString() + "\t" +
//				thisJoinPoint.getSignature().toString() + "\t" );
//		return proceed();
//	}
	
/*	Object around() : call(* com.mysql.jdbc.*.*(..))&& 
			!cflow(within(PreparedStatement_CountCalls)) && !within(edu.uclmgmu.bacteriosec.aspects.common.Store) 
			&& !within(junit.*) {
		Store.updateNumberOfCalls(thisJoinPointStaticPart.getSignature().toString());
		System.out.println("mysql\t" + thisJoinPointStaticPart.getSignature().toString() + "\t" +
				thisJoinPoint.getSignature().toString() + "\t" );
		return proceed();
	}*/
}
